import { useReducer } from "react";
const getDefaultCity = () => {
    let defaultCity = "武汉"
    if (localStorage.getItem("city")) {
        defaultCity = localStorage.getItem("city")
    }
    return defaultCity
}
const useMyReducer = () => {
    const initState = {
        city: getDefaultCity()
    }
    // state不要直接修改，需要做个深拷贝
    const reducer = (state, action) => {
        if (action.type === "changeCity") {
            localStorage.setItem("city", action.value)
            return { city: action.value }
        }
        return initState
    }
    const [state, dispatch] = useReducer(reducer, initState)
    return {state, dispatch}
}

export default useMyReducer